//
//  SearchViewController.m
//  FlowerRainGarden
//
//  Created by 阿仁欧巴 on 16/4/17.
//  Copyright © 2016年 aren. All rights reserved.
//

#import "SearchViewController.h"
#import "FirstModel.h"

@interface SearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UISearchController *searchVC;//搜索框
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *searchArray;

@end

@implementation SearchViewController

//搜索框数组懒加载
- (NSMutableArray *)searchArray
{
    if (!_searchArray) {
        _searchArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _searchArray;
}
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}


- (void)requestData
{
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //    NSString *string = @"action=mainList&cateId=self.ID&currentPageIndex=0&pageSize=5";
    
    
    [session POST:@"http://m.htxq.net/servlet/SysArticleServlet" parameters:@{@"action":@"mainList",@"currentPageIndex":@(0),@"pageSize":@(20), @"cateId":self.ID} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *mDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",mDic);
        NSArray *array = mDic[@"result"];
        for (NSDictionary *dic in array) {
            FirstModel *model = [[FirstModel alloc] init];
            CategoryModel *cateModel = [[CategoryModel alloc] init];
            model.categoryModel = cateModel;
            [cateModel setValuesForKeysWithDictionary:dic[@"category"]];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray  addObject:model];
        }
        
        [SVProgressHUD dismiss];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"搜索中心";
    
    [self requestData];
    [SVProgressHUD show];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
    self.tableView.tableHeaderView = self.searchVC.searchBar;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    
    // Do any additional setup after loading the view from its nib.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    
    return cell;
}



//- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
//{
//    NSString *searchString = [self.searchVC.searchBar text];//搜索的文本信息
//    //过滤器
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
//    //先清空数组
//    if (self.searchArray != nil) {
//        [self.searchArray removeAllObjects];
//    }
//    //过滤数据 再加上数据
//    self.searchArray = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
//    //刷新列表
//    [self.tableView reloadData];
//    
//}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
